<?php

class VocabularyService extends FServiceBase
{
    /**
     * Get a Vocabulary model given its ID
     *
     * @param int id Vocabulary ID
     * @return FServiceModel
     */
    public function get($params)
    {
        $model = Vocabulary::model()->findByPk($this->getParam($params, 'id', 0));
        if (!$model)
            $this->result->fail(ERROR_INVALID_DATA, Yii::t('Admin.Vocabulary', 'Invalid ID.'));
        $this->result->processed('model', $model);
        return $this->result;
    }

    public function save($params)
    {
        /**
         * @var CModel
         */
        $model = $this->getModel($params['Vocabulary'], 'Vocabulary');
        $this->result->processed('model', $model);

        if (!$model->validate())
            $this->result->fail(ERROR_INVALID_DATA, Yii::t('Admin.Vocabulary', 'Submitted data is missing or invalid.'));
        elseif ($this->getParam($params, 'validateOnly', 0) == TRUE)
            return $this->result;
        elseif (!$model->save())
            $this->result->fail(ERROR_HANDLING_DB, Yii::t('Admin.Vocabulary', 'Error while saving submitted data into database.'));

        return $this->result;
    }


    public function delete($params)
    {
        $ids = $this->getParam($params, 'ids', array());
        if ($ids == 0) {
            return $this->result->fail(ERROR_INVALID_DATA, Yii::t('Admin.Vocabulary', 'Invalid ID.'));
        }

        if (!is_array($ids)) $ids = array($ids);
        foreach ($ids as $id) {
            $model = Vocabulary::model()->findByPk($id);
            /**
             * TODO: Check related data if this Vocabulary is deletable
             * This can be done in onBeforeDelete or here or in extensions
             *
            if (Related::model()->count("VocabularyId = {$id}") > 0)
            $this->result->fail(ERROR_VIOLATING_BUSINESS_RULES, Yii::t('Admin.Vocabulary',"Cannot delete Vocabulary ID={$id} as it has related class data."));
            else
             */
            try {
                $model->delete();
            } catch (CDbException $ex) {
                $this->result->fail(ERROR_HANDLING_DB, $ex->getMessage());
            }
        }
        return $this->result;
    }

    public function changeState($params)
    {
        $ids = $this->getParam($params, 'ids', array());
        $value = $this->getParam($params, 'value', false);

        if (count($ids) <= 0)
            return $this->result;

        $criteria = new CDbCriteria();
        $criteria->addInCondition('id', $ids);
        $models = Vocabulary::model()->findAll($criteria);
        foreach ($models as $model) {
            /**
             * @var Vocabulary $model
             */
            $model->state = (boolean)$value;
            if (!$model->save(false, array('state')))
                $this->result->fail(ERROR_HANDLING_DB, 'Error while saving submitted data into database.');
        }

        return $this->result;
    }

    public function findChildren($params)
    {
        $vocabulary = $this->getParam($params, 'id', 0);

        $data = array();
        $criteria = new CDbCriteria();
        $criteria->compare('v_id', $vocabulary);
        $criteria->order = 'ordering';
        $result = Term::model()->findAll($criteria);
        if (count($result)) {
            foreach ($result as $item) {
                $data[] = array(
                    'option' => $item->name,
                    'is_default' => 0,
                    'is_db_live' => "0",
                    'id' => $item->id,
                );
            }
        }

        return $this->result->processed('data', $data);
    }
}